package com.xzt.bookstore.controller;

import com.xzt.bookstore.common.Result;
import com.xzt.bookstore.dto.UserInfoDTO;
import com.xzt.bookstore.entity.Blacklist;
import com.xzt.bookstore.service.UserInfoService;
import com.xzt.bookstore.util.JwtUtil;
import jakarta.servlet.http.HttpServletRequest;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 用户信息控制器
 */
@RestController
@RequestMapping("/api/user-info")
@RequiredArgsConstructor
public class UserInfoController {
    
    private final UserInfoService userInfoService;
    private final JwtUtil jwtUtil;
    
    /**
     * 从请求头中获取用户ID
     */
    private Long getUserIdFromRequest(HttpServletRequest request) {
        String authHeader = request.getHeader("Authorization");
        if (authHeader != null && authHeader.startsWith("Bearer ")) {
            String token = authHeader.substring(7);
            return jwtUtil.getUserIdFromToken(token);
        }
        throw new RuntimeException("未登录");
    }
    
    /**
     * 根据用户ID获取用户信息
     */
    @GetMapping("/{userId}")
    public Result<UserInfoDTO> getUserInfo(@PathVariable Long userId) {
        try {
            UserInfoDTO userInfo = userInfoService.getUserInfo(userId);
            return Result.success(userInfo);
        } catch (RuntimeException e) {
            return Result.error(e.getMessage());
        } catch (Exception e) {
            return Result.error("获取用户信息失败：" + e.getMessage());
        }
    }
    
    /**
     * 根据用户名搜索用户
     */
    @GetMapping("/search")
    public Result<List<UserInfoDTO>> searchUsers(@RequestParam String username) {
        try {
            List<UserInfoDTO> users = userInfoService.searchUsersByUsername(username);
            return Result.success(users);
        } catch (RuntimeException e) {
            return Result.error(e.getMessage());
        } catch (Exception e) {
            return Result.error("搜索用户失败：" + e.getMessage());
        }
    }
    
    /**
     * 拉黑用户
     */
    @PostMapping("/block/{blockedUserId}")
    public Result<Blacklist> blockUser(@PathVariable Long blockedUserId, HttpServletRequest request) {
        try {
            Long userId = getUserIdFromRequest(request);
            Blacklist blacklist = userInfoService.blockUser(userId, blockedUserId);
            return Result.success("拉黑成功", blacklist);
        } catch (RuntimeException e) {
            return Result.error(e.getMessage());
        } catch (Exception e) {
            return Result.error("拉黑失败：" + e.getMessage());
        }
    }
    
    /**
     * 取消拉黑
     */
    @DeleteMapping("/block/{blockedUserId}")
    public Result<String> unblockUser(@PathVariable Long blockedUserId, HttpServletRequest request) {
        try {
            Long userId = getUserIdFromRequest(request);
            userInfoService.unblockUser(userId, blockedUserId);
            return Result.success("取消拉黑成功");
        } catch (RuntimeException e) {
            return Result.error(e.getMessage());
        } catch (Exception e) {
            return Result.error("取消拉黑失败：" + e.getMessage());
        }
    }
    
    /**
     * 获取黑名单列表
     */
    @GetMapping("/blacklist")
    public Result<List<UserInfoDTO>> getBlacklist(HttpServletRequest request) {
        try {
            Long userId = getUserIdFromRequest(request);
            List<UserInfoDTO> blacklist = userInfoService.getBlacklist(userId);
            return Result.success(blacklist);
        } catch (RuntimeException e) {
            return Result.error(e.getMessage());
        } catch (Exception e) {
            return Result.error("获取黑名单失败：" + e.getMessage());
        }
    }
    
    /**
     * 检查用户是否被拉黑
     */
    @GetMapping("/is-blocked/{blockedUserId}")
    public Result<Boolean> isBlocked(@PathVariable Long blockedUserId, HttpServletRequest request) {
        try {
            Long userId = getUserIdFromRequest(request);
            boolean isBlocked = userInfoService.isBlocked(userId, blockedUserId);
            return Result.success(isBlocked);
        } catch (RuntimeException e) {
            return Result.error(e.getMessage());
        } catch (Exception e) {
            return Result.error("检查失败：" + e.getMessage());
        }
    }
}

